package axdoc.rule.check;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import axdoc.model.RuleSelectValue;
import axdoc.rule.check.impl.CheckBlackRule;
import axdoc.rule.check.impl.CheckCityRule;
import axdoc.rule.check.impl.CheckConsultRule;
import axdoc.rule.check.impl.CheckPdeptRule;
import axdoc.rule.check.impl.CheckTitleRule;
import axdoc.util.StringUtils;

public class LoadChecksUtil {
	
	public static Logger logger = LoggerFactory.getLogger("rule_check");

	/**
	 * 转换校验规则实体
	 * 
	 * @param array
	 * @return
	 */
	public static List<AbsCheck> initCheck(JSONArray array) {
		List<AbsCheck> checks = new ArrayList<AbsCheck>();
		try {
			for (int i = 0; i < array.size(); i++) {
				JSONObject obj = array.getJSONObject(i);
				AbsCheck check = (AbsCheck) Class.forName(obj.getString("cname")).newInstance();
				check.initValue(obj);
				checks.add(check);
			}
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
		return checks;
	};

	/**
	 * 将活动规则转换为list形式
	 * 
	 * @param ruleValue
	 * @return
	 */
	public static List<Check> toCheckList(RuleSelectValue ruleValue) {
		List<Check> checks = new ArrayList<Check>();
		if (StringUtils.isNotBlank(ruleValue.getCity())) {
			checks.add(new CheckCityRule(ruleValue.getCity()));
		}
		if (StringUtils.isNotBlank(ruleValue.getDocblack())) {
			checks.add(new CheckBlackRule(Integer.parseInt(ruleValue.getDocblack())));
		}
		if (StringUtils.isNotBlank(ruleValue.getPdept())) {
			checks.add(new CheckPdeptRule(ruleValue.getPdept()));
		}
		if (StringUtils.isNotBlank(ruleValue.getTitle())) {
			checks.add(new CheckTitleRule(ruleValue.getTitle()));
		}
		if (StringUtils.isNotBlank(ruleValue.getPatternType()) && ruleValue.getPatientNum() != null) {
			checks.add(new CheckConsultRule(ruleValue.getPatternType(), ruleValue.getPatientNum()));
		}
		return checks;
	}

}
